#include <iostream>
#include "FriendModel.hpp"
#include "mysql.hpp"


using namespace std;
// {"msgId": 3, "userId": 2, "friendId": "123456"}
void FriendModel::insert(int userId, int friendId) {
    char sql[1024]{0};
    MySql mysql;
    bool c = mysql.connect();
    if (c) {
        sprintf(sql, "INSERT INTO friend VALUES (%d, %d)", userId, friendId);
        mysql.query(sql);
    }
}
vector<User> FriendModel::query(int userId) {
    char sql[1024];
    MySql mysql;
    bool c = mysql.connect();
    vector<User> friends;
    if (c) {
        sprintf(sql, "SELECT b.id, b.name, b.state FROM friend a JOIN user b on a.friendid = b.id where a.userid = %d ORDER BY id;", userId);
        MYSQL_RES* res = mysql.query(sql);
        if (res) {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res))) {
                User user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                friends.push_back(user);
            }
            mysql_free_result(res);
        }
    }
    return friends;
}